<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
</head>
<body>
	<div class="location">
		<strong>你现在所在的位置是:</strong> <span id="title">订单管理页面 &gt;&gt;
			订单添加页面</span>
	</div>
	<div class="providerAdd">
		<form id="bill_form" name="billForm" method="post"
			action="/SMBMS/jsp/bill.do">

			<input type="hidden" id="bill_id" name="id" >
			<div>
				<label for="billCode">订单编码：</label> <input type="text"
					name="billCode" class="text" id="billCode" value=""
					validatestatus="false">
			</div>
			<div>
				<label for="productName">商品名称：</label> <input type="text"
					name="productName" id="productName" value=""> <font
					color="red">*</font>
			</div>
			<div>
				<label for="productUnit">商品单位：</label> <input type="text"
					name="productUnit" id="productUnit" value=""> <font
					color="red">*</font>
			</div>
			<div>
				<label for="productCount">商品数量：</label> <input type="text"
					name="productCount" id="productCount" value=""> <font
					color="red">*</font>
			</div>
			<div>
				<label for="totalPrice">总金额：</label> <input type="text"
					name="totalPrice" id="totalPrice" value=""> <font
					color="red">*</font>
			</div>
			<div>
				<label>供应商：</label> 
				<select id="providerList" name="providerId">
					<option value="0">--请选择--</option>
				</select> <font color="red">*</font>
			</div>
			<div>
				<label>是否付款：</label> <input type="radio" name="isPayment" value="1"
					checked="checked">未付款 <input type="radio" name="isPayment"
					value="2">已付款
			</div>
			<div class="providerAddBtn">
				<input type="submit" name="add" id="add" value="保存"> <input
					type="button" id="back" name="back" value="返回"
					onclick="bill_back()">
			</div>
		</form>
	</div>

	<script>
		function setId(billId) {
			//加载商品详情数据---ajax请求
 			loadProductData(billId);
		}
				 //=========================初始化页面==================================
		/*
		      <select>
		     <option value="">   </option>
		    <option value="">   </option>
		      <option value="">   </option>
		  </select> 
		 */
		loadProviderData();
		//加载供应商	
		function loadProviderData() {
			var $select = $("#providerList");
			var $option = $("#providerList option:eq(0)");

			requestAjax("/SSM/view/provider/getProviderList.htm", "get",
					"json", null, function(data) {
						if (data.success) {
							var providerList = data.result;
							for ( var i in providerList) {
								var id = providerList[i].id;
								var proName = providerList[i].proName;
								var $optionclone = $option.clone(); //创建option模板
								$optionclone.text(proName);
								$optionclone.attr("value",id);
								$select.append($optionclone);
							}
						}
					});
		}
		
		//============================================加载后端数据在页面上==========================
		function loadProductData(billId){
		//发送ajax请求
		requestAjax("/SSM/view/bill/getBillById.htm","get","json",{"id":billId},function(data){
		         //渲染数据
		          renderProductData(data.result);
		    });
		}
		
		//渲染数据
		function renderProductData(result){
		//解析数据
		$("#bill_id").attr("value",result.id);
		$("#billCode").attr("value",result.billCode);
		$("#productName").attr("value",result.productName); 
		$("#productUnit").attr("value",result.productUnit);
	    $("#productCount").attr("value",result.productCount);
	    $("#totalPrice").attr("value",result.totalPrice); 
		var providerId =result.providerId;
		//遍历<option>元素value 匹配后台供应商id
		$("#providerList option").each(function(index,ele){     
		    if($(ele).val()==providerId){
		         $(ele).attr("selected","selected");
		   } ;
		}) ;
		//是否付款
		var isPayment=result.isPayment;
		$("[name='isPayment']:eq(0)").val()==isPayment?$("[name='isPayment']:eq(0)").attr("checked","checked"):$("[name='isPayment']:eq(1)").attr("checked","checked") 
		}
		 
		//===========================提交=================================
		
		 requestAjaxForm("bill_form","/SSM/view/bill/addOrUpdateBill.htm","post","json",function(data){
				if(data.success){
				   //跳到billlist.html
				   $("#content").load("bill_list.html");
				
				} else{
				  layer.msg(data.message);
				}
		 })
		 
		 
		 

		 
		 
		 
		 

		
		
		
		
		
		
		
		
	</script>
</body>
</html>